var loginValidator;
var editor;

var modelObject = {
	init:function(){
		//--表单验证 start--
		loginValidator = $("#addCategoryForm").validate
		({
			rules: {
				name: {
					required:true,
					maxlength:255
				},
				address:{
					required: true,
					maxlength:255
				},
				loginId:{
					required: true,
					maxlength:20,
					loginIdValid: true
				},
				uname:{
					required: true,
					maxlength:10
				},
				loginPwd:{
					required: true,
					minlength:5,
					maxlength:20
				},
				images: "required",
				nav: "required",
				phone: {
					required:true,
					telphoneValid:true
				}
			},
			messages:{
				name:{
					required:"请填写店名",
					maxlength:"产品名称不能超过255个字"
				},
				address:{
					required:"请填写地址",
					maxlength:"产品名称不能超过255个字"
				},
				loginId:{
					required:"请填写登陆ID",
					maxlength:"登陆ID不能超过20个子",
					loginIdValid:"登录ID只能由数字组成"
				},
				uname:{
					required:"请填写用户名",
					maxlength:"用户名不能超过10个字"
				},
				loginPwd:{
					required:"请填写登陆密码",
					maxlength:"密码不能超过20个字",
					minlength:"密码不能小于6个字",
				},
				images:"请上传封面图片",
				nav:"请填写导航",
				phone:{
					required:"请填写电话",
					telphoneValid:"请输入正确的电话"
				}
			},
			submitHandler:function(form){
				//--- 富文本框内容特殊处理 start ---
				editor.sync();
				if (editor.isEmpty()) {
					var dialog = KindEditor.dialog({
						width : 150,
						title : '温馨提示',
						body : '<div style="margin:10px;"><strong>请填写详情描述</strong></div>',
						closeBtn : {
							name : '关闭',
							click : function(e) {
								dialog.remove();
							}
						},
						yesBtn : {
							name : '确定',
							click : function(e) {
								dialog.remove();
							}
						}
					});
					
					return;
				}
				//--- 富文本框内容特殊处理 end ---
				
				form.submit();
			}
		});
		//-- 表单验证 end --
		
		
	}
};

var kindEditors = {
	init: function(){
		//绑定修改图片按钮start
		KindEditor.ready(function(K) {
			var imgEditor = K.editor({
				uploadJson : '../fileUpload',
		        fileManagerJson : '../fileManage',
		        allowImageUpload : true,
		        allowFileManager : true
			});
			$('#image_upload').click(function() {
				imgEditor.loadPlugin('image', function() {
					imgEditor.plugin.imageDialog({
						imageUrl : $('#images').val(),
						clickFn : function(url, title, width, height, border, align) {
							$('#images').val(url);
							$('#img_display').attr('src',url);
							$('#img_display').show();
							$('#image_update').show();
							$('#image_upload').hide();
							imgEditor.hideDialog();
						}
					});
				});
			});
			$('#image_update').click(function() {
				imgEditor.loadPlugin('image', function() {
					imgEditor.plugin.imageDialog({
						imageUrl : $('#image').val(),
						clickFn : function(url, title, width, height, border, align) {
							$('#image').val(url);
							$('#img_display').attr('src',url);
							$('#img_display').show();
							$('#image_update').show();
							$('#image_upload').hide();
							imgEditor.hideDialog();
						}
					});
				});
			});
		});
		//绑定修改图片按钮end
		
		/* 输入框 KindEditor 配置、创建 START*/
		//关闭过滤模式，保留所有标签
		KindEditor.options.filterMode = false;
		editor = KindEditor.create('textarea[name="description"]',{
			 uploadJson : '../fileUpload',
	         fileManagerJson : '../fileManage',
	         allowImageUpload : true,
	         allowFileManager : true
		});
		/* 输入框 KindEditor 配置、创建  END*/
	}
};

$(document).ready(function(){
	jQuery.validator.addMethod("telphoneValid", function(value, element) { 
	    var tel = /^(130|131|132|133|134|135|136|137|138|139|150|153|157|158|159|180|187|188|189)\d{8}$/;
	    var tel1 = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
	    return tel.test(value) || tel1.test(value) || this.optional(element); 
	}, "请输入正确的电话号码");
	jQuery.validator.addMethod("loginIdValid", function(value, element) { 
	    var tel = /^(130|131|132|133|134|135|136|137|138|139|150|153|157|158|159|180|187|188|189)\d{8}$/;
	    var tel1 = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
		var num = /^[0-9]*$/;
	    return num.test(value) || this.optional(element); 
	}, "登录ID只能由纯数字组成");
	
	kindEditors.init();
	modelObject.init();
	
	$("#reset").click(function(){
		$("#images").val("");
		$('#img_display').hide();
		$('#image_update').hide();
		$('#image_upload').show();
	});
});